﻿package com.winglish.as3.loader{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.events.EventDispatcher;
	import flash.events.ProgressEvent;
	import flash.external.ExternalInterface;
	
	import com.winglish.as3.event.TossNumEvent;

	public class SoundPlay extends Sprite {
		private var _url: String;
		private var sodLoader: SoundLoader;
		private var _channel: SoundChannel;
		private var sound: Sound;
		private var _isRepeat: Boolean = false;
		private var num:int;
		private var _bytesLoaded:uint;
		private var _bytesTotal:uint;
		private var pauseTime:Number = 0;
		private var isPause:Boolean = false;
		public var len:Number = 0;
		
		public function SoundPlay() {}

		/**
		 * 사운드 로드.
		 * @param	url 사운드 풀 경로.
		 * @param   num 사운드 그룹을 위한 해당 순서 번호.
		 */
		public function soundLoader( url: String, num:int = 0):void {
			this.num = num;
			_url = url;
			sodLoader = new SoundLoader( url );
			sodLoader.addEventListener( Event.COMPLETE, onCompleteHandler );
			sodLoader.addEventListener( ProgressEvent.PROGRESS, progressEvent);
		}
		
		/**
		 * 사운드 로드 완료 이벤트.
		 * @param	e
		 */
		private function onCompleteHandler( e: Event ):void {
			sound = sodLoader.getResult();
			sodLoader.removeEventListener( ProgressEvent.PROGRESS, progressEvent);
			sodLoader.destroy();
			dispatchEvent( new Event( "loadComplete" ) );
			len = sound.length;
			_channel = sound.play();
			soundStop();
		}
		
		/**
		 * 사운드 로드 진행 이벤트.
		 * @param	e
		 */
		private function progressEvent(e:ProgressEvent):void {
			bytesLoaded = e.bytesLoaded;
			bytesTotal = e.bytesTotal;
			dispatchEvent( new ProgressEvent( ProgressEvent.PROGRESS, false, false, e.bytesLoaded, e.bytesTotal ) );
		}


		/**
		 * 사운드 재생
		 */
		public function soundPlay():void {
			if (isPause) {
				_channel = sound.play(pauseTime);
			}else {
				soundStop();
				_channel = sound.play();
				_channel.addEventListener( Event.SOUND_COMPLETE, soundCompleteHandler );
			}
		}
		
		/**
		 * 특정 위치로 이동해 사운드 재생
		 * @param	num
		 */
		public function soundGotoPlay(num:Number):void {
			isPause = false;
			_channel.removeEventListener( Event.SOUND_COMPLETE, soundCompleteHandler );
			_channel.stop();
			_channel = sound.play(num);
			_channel.addEventListener( Event.SOUND_COMPLETE, soundCompleteHandler );
		}
		
		/**
		 * 사운드 재생 완료 이벤트.
		 * @param	e
		 */
		private function soundCompleteHandler( e: Event ):void {
			if ( _isRepeat ) soundPlay();
			dispatchEvent( new TossNumEvent(TossNumEvent.TOSSNUM, false, true, num) );
			dispatchEvent( new Event(Event.SOUND_COMPLETE) );
		}
		
		/**
		 * 사운드 정지.
		 */
		public function soundStop():void {
			if ( channel ) {
				channel.stop();
				isPause = false;
				channel.removeEventListener( Event.SOUND_COMPLETE, soundCompleteHandler );
			}
			
		}
		
		/**
		 * 사운드 일시정지.
		 */
		public function soundPause():Number {
			if (channel) {
				pauseTime = channel.position;
				channel.stop();
				isPause = true;
			}
			return pauseTime;
		}
		
		/**
		 * 사운드 반복 재생.
		 */
		public function set repeat( value: Boolean ):void {
			_isRepeat = value;
		}
		
		/**
		 * 사운드 url.
		 */
		public function get getUrl():String {
			return _url;
		}
		
		public function get bytesLoaded():uint 
		{
			return _bytesLoaded;
		}
		
		public function get bytesTotal():uint 
		{
			return _bytesTotal;
		}
		
		public function set bytesLoaded(value:uint):void 
		{
			_bytesLoaded = value;
		}
		
		public function set bytesTotal(value:uint):void 
		{
			_bytesTotal = value;
		}
		
		public function get channel():SoundChannel 
		{
			return _channel;
		}
	}
}